package com.teufandsub.socialproximity.logic;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.teufandsub.socialproximity.services.HttpService;

/**
 * @author Bastien Mathias <bastienmathias@gmail.com>
 * @author Antoine Teuf <antoine.teuf@gmail.com>
 * @version 1.0
 * @since 2013-05-22
 * 
 * Message informations, used to store message informations like author, target and content.
 */

public class Message {
	
	private String author;
	private String dest;
	private String content;
	
	public static final String AUTHOR_NAME  = "author";
	public static final String DEST_NAME    = "dest";
	public static final String CONTENT_NAME = "content";
	
	public static final int CODE_SENDED           = 0;
	public static final int CODE_SESSION_INVALID  = 1;
	public static final int CODE_DEST_UNAVAILABLE = 2;
	public static final int CODE_CANT_SEND        = 3;
	public static final int CODE_BLACKLISTED      = 4;
	
	public Message(String author, String dest, String content) {
		super();
		this.dest = dest;
		this.author = author;
		this.content = content;
	}
	
	public String getAuthor() {
		return author;
	}
	
	public void setAuthor(String author) {
		this.author = author;
	}
	
	public String getContent() {
		return content;
	}
	
	
	public String getDest() {
		return dest;
	}

	public void setDest(String dest) {
		this.dest = dest;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	public String toString() {
		return "Message : " + this.author + " - " + this.content;
	}
	
	/**
	 * Method used to send message to the cloud (an other user).
	 * 
	 * @param context Context of the application
	 * @return JSONObject return value of the sending to the cloud.
	 */
	public JSONObject send(Context context) {
		HttpService httpService = new HttpService(context);
		JSONObject ret;
		try {
			ret = httpService.sendMessage(this.toJSONObject());
		} catch (JSONException e) {
			e.printStackTrace();
			ret=null;
		}
		return ret;
	}
	
	/**
	 * Convert message informations to an JSONObject.
	 * 
	 * @return JSONObject informations of message as a JSONObject.
	 * @throws JSONException
	 */
	private JSONObject toJSONObject() throws JSONException {
		JSONObject jSonObject = new JSONObject();
		jSonObject.put(AUTHOR_NAME, this.author);
		jSonObject.put(DEST_NAME, this.dest);
		jSonObject.put(CONTENT_NAME, this.content);
		
		return jSonObject;
	}
}
